//
//  SelectTyreViewController.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/15.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "SelectTyreViewController.h"
#import "XuanTaiData.h"
#import "SelectTyreModel.h"
#import "SelectMyTableViewCell.h"
#import <SDAutoLayout.h>
#import "SelectionTyreCollectionViewCell.h"
#import "SelectTyreCollectionModel.h"

#import "CommodityViewController.h"

#define CollectionURL @"http://lscy4.caeac.com.cn/api/product.php?tid=%@&appkey=1138"

@interface SelectTyreViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray*arrDada;
    NSMutableArray*arrDadaCollection;
    NSMutableArray*arrDataCollectionOne;
    
}
@property(nonatomic, strong)UITableView*tableView;//表格
@property(nonatomic, strong)UICollectionView*collection;//网格

@end

@implementation SelectTyreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //通知传值 (表格)
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getCZ:) name:@"suo" object:nil];
    [[XuanTaiData shardData]getUrlRequest];
    //通知传值 (网格)
    //[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getCZCollection:) name:@"suo1" object:nil];
    
    [self getTableView];//表格
    [self getCollectionView];//网格
    
    //self.automaticallyAdjustsScrollViewInsets = NO;

}

- (void)getCollectionData {
    
    SelectTyreModel *model = arrDada[0];
    NSString *url = [NSString stringWithFormat:CollectionURL,model.sID];
    [[XuanTaiData shardData]getUrlRequestCollection:url seccess:^(id data) {
        
        
        arrDadaCollection = data;
        [_collection reloadData];
        
    }];
}

//通知传值(表格)
-(void)getCZ:(NSNotification*)notificaion{
    arrDada=notificaion.object;
    [_tableView reloadData];
    [self getCollectionData];
}
//通知传值(网格)
-(void)getCZCollection:(NSNotification*)notif{
    arrDadaCollection=notif.object;
    [_collection reloadData];
}
//表格
-(void)getTableView{
    _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 130, self.view.frame.size.height)];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    
    [self.view addSubview:_tableView];
    [self.collection reloadData];
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
}
//网格
-(void)getCollectionView{
    //初始化layout
    UICollectionViewFlowLayout*flow=[[UICollectionViewFlowLayout alloc]init];

    //初始化网格
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(132, 64, self.view.frame.size.width -132, self.view.frame.size.height -70) collectionViewLayout:flow];
    [self.view addSubview:_collection];
    _collection.backgroundColor = [UIColor clearColor];
    //注册collectionViewCell
    [_collection registerClass:[SelectionTyreCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    _collection.delegate=self;
    _collection.dataSource=self;
    
}
#pragma mark collectionView代理方法
//返回section个数(网格)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrDadaCollection.count;
}
//网格的内容设置
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SelectionTyreCollectionViewCell *cell = (SelectionTyreCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
    
    cell.sModel=arrDadaCollection[indexPath.item];
    
    //cell.backgroundColor=[UIColor yellowColor];
    
    return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 142 ), 170);
}
//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 5, 0, 5);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//网格的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    SelectTyreCollectionModel_product *model = arrDadaCollection[indexPath.row];
   
    CommodityViewController *comVC = [[CommodityViewController alloc] init];
    comVC.sid = model.ssID;
   
    
    [self.navigationController pushViewController:comVC animated:YES];
}



#pragma mark 代理方法   tableView代理方法
//表格的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrDada.count;
}
//表格的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectTyreModel *model=arrDada[indexPath.row];
    
    
    return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[SelectMyTableViewCell class] contentViewWidth:[UIScreen mainScreen].bounds.size.width];
}
//表格的内容
-(SelectMyTableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*str=@"biao";
    SelectMyTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell=[[SelectMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    cell.model=arrDada[indexPath.row];
    cell.selectionStyle=UITableViewCellAccessoryNone;
    return cell;
}
//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectTyreModel *model = arrDada[indexPath.row];
    NSString *url = [NSString stringWithFormat:CollectionURL,model.sID];
    
    [[XuanTaiData shardData]getUrlRequestCollection:url seccess:^(id data) {
        
        
        arrDadaCollection = data;
        [_collection reloadData];
        
    }];
}
@end
